// components/chooseImages/index.js
let longTap = 0;
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    fileList: {
      type: Array,
      value: []
    },
    maxlength: {
      type: Number,
      value: -1 //默认不限制
    },
    size: {
      type: String,
      value: "19vw"
    },
    imageFit: {
      type: String,
      value: "aspectFit"
    },

  },

  /**
   * 组件的初始数据
   */
  data: {
    isShake: false,
    activeIndex: null
  },

  /**
   * 组件的方法列表
   */
  methods: {
    showImgs(e) {
      if (longTap === 1) {
        longTap = 2
      }
      if (longTap === 2) {
        longTap = 0
      }
      if (longTap !== 0) {
        return false
      }
      const urls = this.data.fileList.map((item) => {
        return item.url
      })
      wx.previewImage({
        urls: urls,
        current: urls[e.target.dataset.index]
      })
    },
    remove(e) {
      this.triggerEvent("delete", {
        index: e.target.dataset.index
      })
    },
    async logTap(e) {
      if (this.data.isShake) {
        return false;
      }
      longTap = 1;
      try {
        await wx.vibrateLong();
      } catch (error) {}
      this.setData({
        isShake: true,
        activeIndex: e.target.dataset.index
      })
    },
    async chooseImage() {
      if (this.data.fileList.length > 9) {
        wx.showToast({
          title: '图片最多为9张',
          icon: "error",
          mask: true
        })
        return false
      }
      try {
        const imgs = await wx.chooseMedia({
          count: 9,
          mediaType: ["image"]
        });
        const imgArr = imgs.tempFiles.map((item) => {
          return {
            size: item.size,
            thumb: item.tempFilePath,
            type: item.fileType,
            url: item.tempFilePath
          }
        })
        if ((this.data.fileList.length + imgArr.length) > 9) {
          wx.showToast({
            title: '图片最多为9张',
            icon: "error",
            mask: true
          })
          return false
        }
        this.setData({
          isShake: false
        })
        this.triggerEvent("after-read", {
          file: imgArr
        })
      } catch (error) {

      }
    }
  }
})